import { CodeType, transform } from "../commons/ChatUtils";
import StoreUtils from '../commons/StoreUtils';
import { WEBSOCKET } from '../commons/Const'

export default {

  //**********************************************************************
  // 设置state属性方法
  //**********************************************************************

  // 设置appId
  setAppId(state, payload) {
    state.appId = payload
  },

  // 设置是否开启直播
  setLiving: function(state, playload) {
    state.living = playload;
  },

  // 设置websocket
  setWebsocket: function(state, payload) {
    state.websocket = payload;
    StoreUtils.setValue(WEBSOCKET, payload)
  },

  // 设置聊天群中的聊天记录
  setMessageList: function(state, payload) {
    state.messageList = payload;
  },

  //**********************************************************************
  // 扩展方法
  //**********************************************************************

  // 退出后清除内存中的聊天信息
  clear: function(state) {
    state.messageList = [];
  },

  // 保存到内存
  addMessage: function(state, payload) {
    payload.content = transform(payload.content);
    state.messageList.push(payload);
  },

  //**********************************************************************
  // WebSocket相关方法
  //**********************************************************************

  // 发送消息给服务器
  sendMessage: function(state, payload) {
    let msg = {
      code: CodeType.CODE_MESSAGE,
      message: payload
    };
    state.websocket.send(JSON.stringify(msg));
  },

  // 关闭连接
  closeConnect: function(state) {
    state.websocket.heartReset();
    state.websocket.close();
  }

}
